// pages/headline/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav: [{ name: '考试相关' }, { name: '政策解读' }],
    currentId: 0,
    news: [],
    page: 1,
    pagesize: '',
    endloading: false,
    type: 1,
    id: ""//顶部分类id
  },
  current(e) {
    this.setData({
      currentId: e.currentTarget.id,
    })
    if (e.currentTarget.id == 1) {
      this.setData({
        type: 2,
        page: 1
      })
      this.getList()
    } else {
      this.setData({
        type: 1,
        page: 1
      })
      this.getList()
    }
  },
  // 分类选择
  sort() {
    wx.navigateTo({
      url: '/pages/sort/index',
    })
  },
  newsDetails(e) {
    wx.navigateTo({
      url: '/pages/newsDetails/index?id=' + e.currentTarget.dataset.id,
    })
  },
  liveDetails(e) {
    let idx = e.currentTarget.id
    let arrIdx = e.currentTarget.dataset.index
    let type = this.data.news[arrIdx].zhi[idx].type
    let id = this.data.news[arrIdx].zhi[idx].id
    let pinAllow = this.data.news[arrIdx].zhi[idx].collage_allow
    if (type == 1 && pinAllow == 0) {
      wx.navigateTo({
        url: '/pages/liveDetails/index?id=' + id,
      })
    } else if (type == 1 && pinAllow == 1) {
      wx.navigateTo({
        url: '/pages/livePt/index?id=' + id,
      })
    }
  },
  recDetails(e) {
    console.log(e)
    let idx = e.currentTarget.id
    let arrIdx = e.currentTarget.dataset.id
    let pinAllow = this.data.news[arrIdx].lu[idx].collage_allow
    let id = this.data.news[arrIdx].lu[idx].id
    if (pinAllow == 0) {
      wx.navigateTo({
        url: '/pages/recDetails/index?id=' + id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/recPt/index?id=' + id,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getList() {
    let data = {
      method: 'article.getlist',
      page: this.data.page,
      news_cate_id: this.data.type,
      cate_id: this.data.id.id
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          pagesize: res.data.last_page
        })
        var datas = this.data.news
        if (this.data.page == 1) {
          datas = []
        }
        if (res.data.list.lenght == 0) {
          this.setData({
            endloading: true, //上拉不在加载
          })
        } else {
          this.setData({
            page: this.data.page + 1 //更新page 请求下一页数据
          })
          this.setData({
            news: datas.concat(res.data.list),
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    wx.setBackgroundTextStyle({
      textStyle: 'dark',// 下拉背景字体、loading 图的样式为dark
    })
    wx.setBackgroundColor({
      backgroundColor: '#666666', // 窗口的背景色为白色
    })
    this.setData({
      page: 1,
      id: ''
    })
    setTimeout(() => {
      this.getList()
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (this.data.page <= this.data.pagesize) {
      wx.showLoading({
        title: '加载中'
      })
      setTimeout(() => {
        wx.hideLoading({})
        that.getList()  //页面上拉调用这个方法
      }, 1000)
    } else {
      wx.showToast({
        title: '我已经到底了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})